﻿<?php
/*
 * Created on 2011-3-5 by Falcon 
 * 
 * http://www.falcon-chen.tk 
 * admin@falcon-chen.tk
 *  
  */
  $siteRoot = dirname(__FILE__);
 define( "SITE_ROOT",$siteRoot);
 define ("FILES_ROOT",SITE_ROOT . '/files');
 //增加可移植性
 $scriptUri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
 $baseName = basename($_SERVER['PHP_SELF']);
 $domain = rtrim(str_replace($baseName,'',$scriptUri),'/');
 define ('DOMAIN',$domain);
 
  session_start();
  try {
	  if ( isset($_GET['id']) && $_GET['id'] !=null) {
	  	$fileId =$_GET['id'];
	  	$fileName = $_SESSION['file'][$fileId];
	  	//var_dump($fileName);
		//die();
		//$fileName = iconv('utf-8','gbk',$fileName);
	  	if(!file_exists(FILES_ROOT.'/'.$fileName)) throw new Exception("file doesn't exist". FILES_ROOT.'/'.$fileName);
	  	
	  	 //读取内容并生成下载
	  	  $file = FILES_ROOT.'/'. $fileName;
	      header("Content-length: ".filesize($file));
	      header('Content-type: application/'.filetype($file));
		  header("Content-Disposition: attachment; filename={$fileName}");
	      readfile($file);
		 // unlink($file);
	
	  	
	    //$fileDownUrl = DOMAIN . '/files/' . $fileName; 	 
	  }else {
	  	throw new Exception('empty id .');
	  }
	   
  }catch (Exception $e) {
  	 
  	 echo 'Sorry,something wrong happened : ',$e->getMessage();
  	 echo "<br /><a href ='". DOMAIN ."'>返回</a>";
  }
   
 
 
 

 
  
?>
